package com.luoke.dao.impl;

import com.luoke.dao.AttributeDao;
import com.luoke.pojo.entity.Attribute;
import com.luoke.util.DatabaseUtil;

import java.sql.*;

public class AttributeDaoImpl implements AttributeDao {

    @Override
    public Attribute getPetAttributeByPetName(String newPetName) {
        String query = "SELECT attribute.id, attribute.power, attribute.intelligence, attribute.dexterity, attribute.accuracy, attribute.defence, attribute.main " +
                "FROM pet " +
                "JOIN attribute ON pet.attribute_id = attribute.id " +
                "WHERE pet.name = ?";
        Attribute attribute = new Attribute();
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, newPetName); // 绑定宠物名
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    attribute.setId(resultSet.getLong("id"));
                    attribute.setPower(resultSet.getInt("power"));
                    attribute.setIntelligence(resultSet.getInt("intelligence"));
                    attribute.setDexterity(resultSet.getInt("dexterity"));
                    attribute.setAccuracy(resultSet.getInt("accuracy"));
                    attribute.setDefence(resultSet.getInt("defence"));
                    attribute.setMain(resultSet.getString("main"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return attribute;
    }

    @Override
    public int getPetLevelByPetName(String newPetName, String petName) {
        String query = "SELECT up.level " +
                "FROM user u " +
                "JOIN user_pet up ON u.id = up.user_id " +
                "JOIN pet p ON up.pet_id = p.id " +
                "WHERE u.name = ? AND p.name = ?";

        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, newPetName);
            statement.setString(2, petName);

            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    return resultSet.getInt("level");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }



        return 0;
    }
}
